$(function () {
    $("#messageDialog").dialog({
        autoOpen:false,
        modal:true,
        show:"slide",
        buttons:{
            "<{$lang.ok}>":function () {
                $(this).dialog("close");
            }
        }
    });
    $("#confirmDialog").dialog({
        autoOpen:false,
        modal:true,
        show:"slide",
        buttons:{
            "<{$lang.ok}>":function () {
                receiving();
                $(this).dialog("close");
            },
            "<{$lang.cancel}>":function () {
                $(this).dialog("close");
            }
        }
    });
    $("#content").height($(document).height());
    $("#receiveDialog").dialog({
        autoOpen:false,
        modal:true,
        show:"slide",
        buttons:{
            "<{$lang.ok}>":function () {
                if ($.trim($("[name=remark]").val()) != "") {
                    $("#confirmDialog").html("<{$lang.confirm}><{$lang.exists}><{$lang.exception}>?").dialog("open");
                    return;
                }
                receiving();
            },
            "<{$lang.cancel}>":function () {
                $(this).dialog("close");
            }
        }
    });

    $(".receive").click(function () {
        $(".<{$lang.sku}>").html($(".<{$lang.sku}>" + $(this).attr("itemid")).html());
        $(".title").html($(".title" + $(this).attr("itemid")).html());
        $(".category").html($(".category" + $(this).attr("itemid")).html());
        $(".qty").html($(".qty" + $(this).attr("itemid")).html());
        $(".weight").html($(".weight" + $(this).attr("itemid")).html());
        $("[name=itemId]").val($(this).attr("itemid"));

        $("[name=weight]").val($(".weight" + $(this).attr("itemid")).html());		//实际<{$lang.weight}> 等于 <{$lang.standard}><{$lang.weight}>

        $("[name=receiveQty]").val("");
        $("[name=nonLabelQty]").val("0");
        $("[name=remark]").val("");
        $("[name=isPacket][value=0]").attr("checked", true);
        $("#receiveDialog").dialog("open");
    });
});
function receiving() {
    var data = $("#editpform").serializeArray();
    $.ajax({
        url:"/product/receiving/receive-add",
        type:"post",
        dataType:"json",
        async:false,
        data:data,
        success:function (json) {
            var itemId = $("[name=itemId]").val();
            if (json.ask) {
                $("#receiveQty" + itemId).html(json.item.received_qty);
                $("#weight" + itemId).html(json.item.weight);
                $("#nonLabled" + itemId).html(json.item.non_labeled_qty);
                $("#packaged" + itemId).html(json.item.packaged);
                $("#remark" + itemId).html(json.item.remark);
                $(".receive[itemid=" + itemId + "]").parent().html("<img src='/images/icon/normal.gif' title='<{$lang.received}>'/>");
                $("#receiveDialog").dialog("close");
            } else {
                $("#messageDialog").html(json.message).dialog("open");
            }
        }
    });
}

//<{$lang.batch}><{$lang.submit}>
$(function () {
    $(".submitbatch").click(function () {
        var chking = chk();
        if (chking == 0) {
            return;
        } else if (chking == 2) {
            confirmDialog('<{$lang.confirm}><{$lang.exists}><{$lang.exception}>?');
            return;
        }
        receivingbatch();
    });
})
//
function receivingbatch() {
    var data = $("#batchform").serializeArray();
    $.ajax({
        url:"/product/receiving/receive-add-batch",
        type:"post",
        dataType:"json",
        async:false,
        data:data,
        success:function (json) {
            if (json.ask) {
                alertTip(json.result);
                window.location.reload();
            } else {
                $("#messageDialog").html(json.error).dialog("open");
            }
        }
    });
}


function chk() {
    var msg = '';
    var abnormal = false;
    $(".receiveQty", "#batchform").each(function (k, v) {
        var ri_id = $(this).attr("app");
        var b_val = $("[name=" + "\'isBatch[" + ri_id + "]\']").val();
        var pw_val = $("[name=" + "\'product_weight[" + ri_id + "]\']").val() * 1;//<{$lang.standard}><{$lang.weight}>
        var w_val = $("[name=" + "\'weight[" + ri_id + "]\']").val() * 1;//实际
        var o_val = $("[name=" + "\'orderQty[" + ri_id + "]\']").val() * 1;//应收
        var r_val = $("[name=" + "\'receiveQty[" + ri_id + "]\']").val() * 1;
        var n_val = $("[name=" + "\'nonLabelQty[" + ri_id + "]\']").val() * 1;
        var remark_val = $("[name=" + "\'remark[" + ri_id + "]\']").val();
        var <{$lang.sku}> = $(".<{$lang.sku}>" + ri_id).html();
        if ($("#isBatch_" + ri_id).is(":checked")) {
            if (o_val - r_val == 0) {
                msg += "<span><{$lang.sku}>: " + <{$lang.sku}> + "</span>";
                msg += "<span><img src=\"/images/no.gif\"><{$lang.shishou_yinshou_not_fenpi}>!</span>"
            }
        } else {
            if (r_val < o_val && remark_val == '') {
                msg += "<span><{$lang.sku}>: " + <{$lang.sku}> + "</span>";
                msg += "<span><img src=\"/images/no.gif\"><{$lang.receive_qty_less_than_receive_qty_not_fenpi}>!</span>"
            }
            if (r_val < o_val && remark_val != '') {
                abnormal = true;
            }
        }
        if (n_val > r_val) {
            msg += "<span><{$lang.sku}>: " + <{$lang.sku}> + "</span>";
            msg += "<span><img src=\"/images/no.gif\"><{$lang.no_label_quantity_can_not_more_than_receive}>!</span>"
        }
        if (Math.abs((pw_val - w_val)) > 0.02 && remark_val == '') {
            msg += "<span><{$lang.sku}>: " + <{$lang.sku}> + "</span>";
            msg += "<span><img src=\"/images/no.gif\"><{$lang.weight_more_than_0_02_need_reason}>!</span>"
        }
    });
    if (msg != '') {
        alertTip(msg);
        msg = '';
        return 0;
    }
    if (abnormal) {
        return 2;
    }
    return 1;
}

function onblurqty(obj, qty) {
    var this_val = $(obj).val();
    var pattern = /^\d+$/;//匹配正整数
    if (!pattern.test(this_val)) {
        alertTip("<{$lang.must_integer}>!");
        $(obj).val(0);
        return;
    }
    if (this_val > qty) {
        alertTip("<{$lang.can_not_more_than_act_qty}>!");
        $(obj).val(0);
        return;
    }
}


function onblurweight(obj, qty) {
    var this_val = $(obj).val();
    var pattern = /^[\+]?\d*?\.?\d*?$/;
    if (!pattern.exec(this_val)) {
        alertTip("<{$lang.must_integer}>!!");
        $(obj).val(qty);
        return;
    }
}

function confirmDialog(tip) {
    $('<div title="<{$lang.confirm}><{$lang.tip}>" class="alertTip"><p align="center">' + tip + '</p></div>').dialog({
        modal:true,
        width:450,
        buttons:{
            '<{$lang.ok}>':function () {
                receivingbatch();
                $(this).dialog("close");
            },
            '<{$lang.cancel}>':function () {
                $(this).dialog("close");
            }
        }
    });
}


//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>" class="alertTip"><p align="center">' + tip + '</p></div>').dialog({
        modal:true,
        width:450,
        buttons:{
            '<{$lang.ok}>':function () {
                $(this).dialog("close");
            }
        }
    });
}